package com.tancw.web;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import com.tancw.bean.BaseResult;
import com.tancw.bean.MenuAPI;

@Controller
public class HomeController {

    @GetMapping("/")
    public String index(Model model) throws IOException {
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("menu.json");
        String menu = IOUtils.toString(in, "UTF-8");
        model.addAttribute("menu", menu);
        return "index";
    }


    @GetMapping("/home")
    public String home(){
        return "home";
    }

    @PostMapping("menuSubmit")
    public String submit(String accToken, String menujson, Model model) {
        BaseResult res = MenuAPI.menuCreate(accToken, menujson);
        System.out.println(res);
        if (res.isSuccess()) {
            model.addAttribute("message", "成功");
        } else {
            model.addAttribute("message", "失败:" + res.getErrmsg());
        }
        return "message";
    }
}
